package com.zms.maze;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class IOUtil {
	
	public static void writeMazeScript(Maze maze,int num,PrintWriter out) {		
		out.println("<script language=\"javascript\">");
		out.println("\tmummynum="+maze.mummies.size()+";");
		if(maze.mummies.size()>0) {
			out.print("\tmummytypes=[");
			out.print(maze.mummies.get(0).type.ordinal());		
			for(int i=1;i<maze.mummies.size();i++) {
				out.print(","+maze.mummies.get(i).type.ordinal());
			}
			out.println("];");
			out.print("\tmummyweights=[");
			out.print(maze.mummies.get(0).weight);		
			for(int i=1;i<maze.mummies.size();i++) {
				out.print(","+maze.mummies.get(i).weight);
			}
			out.println("];");
		}
		out.println("\tthisnum="+num+";");
		out.println("\tstartx="+Maze.StartX+";");
		out.println("\tstarty="+Maze.StartY+";");
		out.println("\tblockwidth="+Maze.BlockWidth+";");
		out.println("\tblockheight="+Maze.BlockHeight+";");
		out.println("\tblockcolumns="+maze.blockcolumns+";");
		out.println("\tblockrows="+maze.blockrows+";");
		out.println("\tdoorx="+maze.doorx+";");
		out.println("\tdoory="+maze.doory+";");
		out.println("\tlinenum="+maze.lines.size()+";");
		out.println("\ttogglenum="+maze.toggles.size()+";");
		out.println("\ttrapnum="+maze.traps.size()+";");
		out.println("\tscorpionnum="+maze.scorpions.size()+";");
		if(maze.scorpions.size()>0) {
			out.print("\tscorpionweights=[");
			out.print(maze.scorpions.get(0).weight);		
			for(int i=1;i<maze.scorpions.size();i++) {
				out.print(","+maze.scorpions.get(i).weight);
			}
			out.println("];");
		}
		if((maze.lines.size()+maze.toggles.size())>0) {
			Line line;
			int k=0;
			if(maze.lines.size()>0) {
				line=maze.lines.get(0);
			} else {
				k=1;
				line=maze.toggles.get(0);
			}
			String x1="linex1=["+line.x1;
			String x2="linex2=["+line.x2;
			String y1="liney1=["+line.y1;
			String y2="liney2=["+line.y2;			
			for(int i=1;i<maze.lines.size();i++) {
				line=maze.lines.get(i);
				x1+=","+line.x1;
				x2+=","+line.x2;
				y1+=","+line.y1;
				y2+=","+line.y2;
			}
			for(int i=k;i<maze.toggles.size();i++) {
				line=maze.toggles.get(i);
				x1+=","+line.x1;
				x2+=","+line.x2;
				y1+=","+line.y1;
				y2+=","+line.y2;
			}
			out.println("\t"+x1+"];");
			out.println("\t"+x2+"];");
			out.println("\t"+y1+"];");
			out.println("\t"+y2+"];");
		}
		if(maze.toggles.size()>0) {
			ToggleLine line=maze.toggles.get(0);
			String x="togglex=["+line.togglex;
			String y="toggley=["+line.toggley;
			String s="toggles=["+line.state;
			for(int i=1;i<maze.toggles.size();i++) {
				line=maze.toggles.get(i);
				x+=","+line.togglex;
				x+=","+line.toggley;
				s+=","+line.state;				
			}
			out.println("\t"+x+"];");
			out.println("\t"+y+"];");
			out.println("\t"+s+"];");
		}
		if(maze.traps.size()>0) {
			Trap trap=maze.traps.get(0);
			String x="trapx=["+trap.startx;
			String y="trapy=["+trap.starty;
			for(int i=1;i<maze.traps.size();i++) {
				trap=maze.traps.get(i);
				x+=","+trap.startx;
				y+=","+trap.starty;				
			}
			out.println("\t"+x+"];");
			out.println("\t"+y+"];");
		}
		out.println("</script>");
		out.println("<script language=\"javascript\" src=\"maze.js\"></script>");
	}
	
	private static void writeLine(Maze maze,Line line,PrintWriter out) {
		if(line.x1==line.x2) {
			for(int i=line.y1;i<line.y2;i++) {
				if(i==(maze.blockrows-1)) {
					out.println("<img src=\"images/wallc.gif\" width=\""+Maze.WallWidthc+"\" height=\""+Maze.WallHeightc+"\" style=\"position:absolute;left="+(Maze.StartX+line.x1*Maze.BlockWidth-Maze.WallWidtha/4)+";top="+(Maze.StartY+i*Maze.BlockHeight-(Maze.WallHeightc-Maze.BlockHeight))+"\"/>");
				} else {
					out.println("<img src=\"images/walla.gif\" width=\""+Maze.WallWidtha+"\" height=\""+Maze.WallHeighta+"\" style=\"position:absolute;left="+(Maze.StartX+line.x1*Maze.BlockWidth-Maze.WallWidtha/4)+";top="+(Maze.StartY+i*Maze.BlockHeight-(Maze.WallHeighta-Maze.BlockHeight-Maze.WallWidtha/4))+"\"/>");
				}
			}
		}
		if(line.y1==line.y2) {
			for(int i=line.x1;i<line.x2;i++) {
				out.println("<img src=\"images/wallb.gif\" width=\""+Maze.WallWidthb+"\" height=\""+Maze.WallHeightb+"\" style=\"position:absolute;left="+(Maze.StartX+i*Maze.BlockWidth-Maze.WallHeightb/6)+";top="+(Maze.StartY+line.y1*Maze.BlockHeight-Maze.WallHeightb*5/6)+"\"/>");
			}
		}
	}
	public static void writeMaze(Maze maze,PrintWriter out) {
		for(int i=1;i<=maze.blockrows;i++) {
			for(int j=1;j<=maze.blockcolumns;j++) {
				out.print("<img src=\"");
				if(((i+j) % 2)==0) {
					out.print("images/floor1.gif");
				} else {
					out.print("images/floor2.gif");
				}
				out.println("\" width=\""+Maze.BlockWidth+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX+(j-1)*Maze.BlockWidth)+";top="+(Maze.StartY+(i-1)*Maze.BlockHeight)+"\"/>");
			}
		}
		out.println("<img src=\"images/upborder.gif\" width=\""+Maze.BlockWidth*maze.blockcolumns+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX)+";top="+(Maze.StartY-Maze.BlockHeight)+"\"/>");
		out.println("<img src=\"images/downborder.gif\" width=\""+Maze.BlockWidth*(maze.blockcolumns+2)+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX-Maze.BlockWidth)+";top="+(Maze.StartY+Maze.BlockHeight*maze.blockrows)+"\"/>");
		out.println("<img src=\"images/leftborder.gif\" width=\""+Maze.BlockWidth+"\" height=\""+Maze.BlockHeight*maze.blockrows+"\" style=\"position:absolute;left="+(Maze.StartX-Maze.BlockWidth)+";top="+(Maze.StartY)+"\"/>");
		out.println("<img src=\"images/rightborder.gif\" width=\""+Maze.BlockWidth+"\" height=\""+(Maze.BlockHeight*maze.blockrows+3)+"\" style=\"position:absolute;left="+(Maze.StartX+Maze.BlockWidth*maze.blockcolumns)+";top="+(Maze.StartY)+"\"/>");
		
		out.println("<img src=\"images/upleftborder.gif\" width=\""+Maze.BlockWidth+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX-Maze.BlockWidth)+";top="+(Maze.StartY-Maze.BlockHeight)+"\"/>");
		out.println("<img src=\"images/uprightborder.gif\" width=\""+Maze.BlockWidth+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX+Maze.BlockWidth*maze.blockcolumns)+";top="+(Maze.StartY-Maze.BlockHeight)+"\"/>");
		for(Trap trap:maze.traps) {
			out.println("<img src=\"images/trap.gif\" width=\""+Maze.BlockWidth+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX+trap.startx*Maze.BlockWidth)+";top="+(Maze.StartY+trap.starty*Maze.BlockHeight)+"\"/>");
		}
		String door="blank.gif";
		if(maze.doorx==maze.blockcolumns) {
			door="doorright.gif";
		} else if(maze.doory==maze.blockrows) {
			door="doordown.gif";
		} else if(maze.doorx==-1) {
			door="doorleft.gif";
		} else if(maze.doory==-1) {
			door="doorup.gif";
		}
		out.println("<img src=\"images/"+door+"\" width=\""+Maze.BlockWidth+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX+maze.doorx*Maze.BlockWidth)+";top="+(Maze.StartY+maze.doory*Maze.BlockHeight)+"\"/>");
		
		List<Line> lines=new ArrayList<Line>();
		lines.addAll(maze.lines);
		lines.addAll(maze.toggles);
		Maze.sortLines(lines);
		for(Line line:lines) {
			writeLine(maze,line,out);
		}
		int n=0;
		for(ToggleLine line:maze.toggles) {
			out.println("<img src=\"images/key.gif\" width=\""+Maze.KeyWidth+"\" height=\""+Maze.KeyHeight+"\" style=\"position:absolute;left="+(Maze.StartX+line.togglex*Maze.BlockWidth+(Maze.BlockWidth-Maze.KeyWidth)/2)+";top="+(Maze.StartY+line.toggley*Maze.BlockHeight+(Maze.BlockHeight-Maze.KeyHeight)/2)+"\"/>");			
			if(line.y1==line.y2) {
				String image;
				if(line.state==1) {
					image="toggle1c.gif";
				} else {
					image="toggle1o.gif";
				}
				out.println("<img id=\"toggle"+(n++)+"\" width=\""+Maze.ToggleWidthb+"\" height=\""+Maze.WallHeightb+"\" src=\"images/"+image+"\" style=\"position:absolute;left="+(Maze.StartX+line.x1*Maze.BlockWidth+(Maze.BlockWidth-Maze.ToggleWidthb)/2)+";top="+(Maze.StartY+line.y1*Maze.BlockHeight-Maze.WallHeightb*5/6)+"\"/>");
			}
		}
		
		String none="\"display:none\"";
		if(maze.toggles.size()>0) {
			out.println("<img id=\"toggle1c\" src=\"images/toggle1c.gif\" style="+none+"/>");
			out.println("<img id=\"toggle1o\" src=\"images/toggle1o.gif\" style="+none+"/>");
			out.println("<img id=\"toggle2c\" src=\"images/toggle2c.gif\" style="+none+"/>");
			out.println("<img id=\"toggle2o\" src=\"images/toggle2o.gif\" style="+none+"/>");
		}
		out.println("<img id=\"manleft\" src=\"images/manleft.gif\" style="+none+"/>");
		out.println("<img id=\"manright\" src=\"images/manright.gif\" style="+none+"/>");
		out.println("<img id=\"manup\" src=\"images/manup.gif\" style="+none+"/>");
		out.println("<img id=\"mandown\" src=\"images/mandown.gif\" style="+none+"/>");
		
		if(maze.scorpions.size()>0) {
			out.println("<img id=\"scorpiondown\" src=\"images/scorpiondown.gif\" style="+none+"/>");
			out.println("<img id=\"scorpionup\" src=\"images/scorpionup.gif\" style="+none+"/>");
			out.println("<img id=\"scorpionleft\" src=\"images/scorpionleft.gif\" style="+none+"/>");
			out.println("<img id=\"scorpionright\" src=\"images/scorpionright.gif\" style="+none+"/>");
		}
		
		out.println("<img id=\"man\" src=\"images/mandown.gif\" width=\""+Maze.BlockWidth+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX+maze.manx*Maze.BlockWidth)+";top="+(Maze.StartY+maze.many*Maze.BlockHeight)+"\"/>");
		boolean haswhite=false;
		boolean hasred=false;
		n=0;
		for(Mummy mummy:maze.mummies) {
			out.print("<img id=\"mummy"+(n++)+"\" src=\"");
			if(mummy.type==MummyType.White) {
				haswhite=true;
				out.print("images/whitedown.gif");
			} else {
				hasred=true;
				out.print("images/reddown.gif");
			}
			out.println("\" width=\""+Maze.BlockWidth+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX+mummy.startx*Maze.BlockWidth)+";top="+(Maze.StartY+mummy.starty*Maze.BlockHeight)+"\"/>");
		}
		if(haswhite) {
			out.println("<img id=\"whitedown\" src=\"images/whitedown.gif\" style="+none+"/>");
			out.println("<img id=\"whiteup\" src=\"images/whiteup.gif\" style="+none+"/>");
			out.println("<img id=\"whiteleft\" src=\"images/whiteleft.gif\" style="+none+"/>");
			out.println("<img id=\"whiteright\" src=\"images/whiteright.gif\" style="+none+"/>");
			out.println("<img id=\"whitedie\" src=\"images/die1.gif\" style="+none+"/>");
		}
		if(hasred) {
			out.println("<img id=\"reddown\" src=\"images/reddown.gif\" style="+none+"/>");
			out.println("<img id=\"redup\" src=\"images/redup.gif\" style="+none+"/>");
			out.println("<img id=\"redleft\" src=\"images/redleft.gif\" style="+none+"/>");
			out.println("<img id=\"redright\" src=\"images/redright.gif\" style="+none+"/>");
			out.println("<img id=\"reddie\" src=\"images/die2.gif\" style="+none+"/>");
		}
		out.println("<img id=\"blank\" src=\"images/blank.gif\" style="+none+"/>");
		
		for(Scorpion scorpion:maze.scorpions) {
			out.println("<img src=\"images/scorpiondown.gif\" width=\""+Maze.BlockWidth+"\" height=\""+Maze.BlockHeight+"\" style=\"position:absolute;left="+(Maze.StartX+scorpion.startx*Maze.BlockWidth)+";top="+(Maze.StartY+scorpion.starty*Maze.BlockHeight)+"\"/>");
		}		
	}
}
